﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.Xml.Linq;
using System.Web.Services;
using Telerik.Web.UI;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.MODEL;

namespace KPIS.GERP.GBMS.APP_UI.Form
{
    public partial class BGM_Project_Expense_Plan : System.Web.UI.Page
    {

        private IProject projectBLL;
        private ComboBLL comboBLL = new ComboBLL();
        private SystemTypeYearBLL sysTypeYearBLL = new SystemTypeYearBLL();
        private string type;
        string[] strMon = { "ม.ค.", "ก.พ.", "มี.ค.", "เม.ย.", "พ.ค.", "มิ.ย.", "ก.ค.", "ส.ค.", "ก.ย.", "ต.ค.", "พ.ย.", "ธ.ค." };
        string[] strMonth = { "มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม" };

        protected void Page_Load(object sender, EventArgs e)
        {
            type = Request.QueryString["type"].ToString();

             if (type.Length > 0)
                projectBLL = Project_Factory.GetProjectFactory(type);

            lbProjSeq.Text = Convert.ToString(Session["ProjectID"]);
            
            if (!IsPostBack)
            {
                LoadData();
            }

            if (!Object.Equals(Session["ProjectName"], null))
            {
                litTitle.Text = Session["ProjectName"].ToString();
            }
        }

        private void LoadData()
        {
            DataRow dr = sysTypeYearBLL.GetDataSystemTypeYear().Rows[0];
            lblMonthType.Text = dr["month"].ToString();

            DataTable dt = projectBLL.GetDataProjectExpensePlan(lbProjSeq.Text, "");
            Grid_t4.DataSource = dt;

            Grid_t4.DataBind();
            Grid_t4.Visible = true;
            panelGrid_t4.Visible = true;
        }

        private void clear_t4()
        {
            txtMonthT4_1.Text = "";
            txtMonthT4_2.Text = "";
            txtMonthT4_3.Text = "";
            txtMonthT4_4.Text = "";
            txtMonthT4_5.Text = "";
            txtMonthT4_6.Text = "";
            txtMonthT4_7.Text = "";
            txtMonthT4_8.Text = "";
            txtMonthT4_9.Text = "";
            txtMonthT4_10.Text = "";
            txtMonthT4_11.Text = "";
            txtMonthT4_12.Text = "";
            PanelAddEdit_t4.Visible = false;
        }
        private void bindTab4_forEdit(string seq)
        {
            string[] str = seq.Split('|');  // 0=datakey   1=refkey
            DataRow dr = projectBLL.GetDataProjectExpensePlanBySequence(str[0]).Rows[0];
            hdfTotal_t4.Value = dr["total_amount"].ToString();
            txtMonthT4_1.Text = dr["amount_month1"].ToString();
            txtMonthT4_2.Text = dr["amount_month2"].ToString();
            txtMonthT4_3.Text = dr["amount_month3"].ToString();
            txtMonthT4_4.Text = dr["amount_month4"].ToString();
            txtMonthT4_5.Text = dr["amount_month5"].ToString();
            txtMonthT4_6.Text = dr["amount_month6"].ToString();
            txtMonthT4_7.Text = dr["amount_month7"].ToString();
            txtMonthT4_8.Text = dr["amount_month8"].ToString();
            txtMonthT4_9.Text = dr["amount_month9"].ToString();
            txtMonthT4_10.Text = dr["amount_month10"].ToString();
            txtMonthT4_11.Text = dr["amount_month11"].ToString();
            txtMonthT4_12.Text = dr["amount_month12"].ToString();
        }

        protected void Grid_t4_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridHeaderItem)
            {
                int monType = int.Parse(lblMonthType.Text);
                int tmp;
                GridHeaderItem header = (GridHeaderItem)e.Item;
                string strColumn;
                for (int i = 0; i < 12; i++)
                {
                    tmp = i + monType;
                    if (tmp > 12)
                        tmp = tmp - 12;
                    strColumn = "amount_month" + (i + 1).ToString();
                    header[strColumn].Text = strMonth[tmp - 1];

                    Label lbl = (Label)PanelAddEdit_t4.FindControl("lblMonthT4_" + (i + 1).ToString());
                    lbl.Text = strMonth[tmp - 1] + ":";
                }
            }
        }

        protected void Grid_t4_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.EditCommandName)
            {
                clear_t4();
                PanelAddEdit_t4.Visible = true;
                string key = (e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"].ToString();
                Label lb = Grid_t4.Items[e.Item.ItemIndex].FindControl("lbProjectExpSeq") as Label;
                //total_amount
                lblTotalAmount.Text = Grid_t4.Items[e.Item.ItemIndex]["total_amount"].Text;
                if (key != "")
                {
                    lblSeq_t4.Text = key;
                    lblSeq_t4.Text += "|" + lb.Text;
                    bindTab4_forEdit(lblSeq_t4.Text);
                }
                else
                {
                    lblSeq_t4.Text = lb.Text;
                }
                txtMonthT4_1.Focus();
            }
            LoadData();
        }

        protected void btnCancel_t4_Click(object sender, EventArgs e)
        {
            clear_t4();
        }

        protected void btnEdit_t4_Click(object sender, EventArgs e)
        {
            double totalTxtMonth = txtMonthT4_1.Text.Length > 0 ? double.Parse(txtMonthT4_1.Text) : 0;
            totalTxtMonth += txtMonthT4_2.Text.Length > 0 ? double.Parse(txtMonthT4_2.Text) : 0;
            totalTxtMonth += txtMonthT4_3.Text.Length > 0 ? double.Parse(txtMonthT4_3.Text) : 0;
            totalTxtMonth += txtMonthT4_4.Text.Length > 0 ? double.Parse(txtMonthT4_4.Text) : 0;
            totalTxtMonth += txtMonthT4_5.Text.Length > 0 ? double.Parse(txtMonthT4_5.Text) : 0;
            totalTxtMonth += txtMonthT4_6.Text.Length > 0 ? double.Parse(txtMonthT4_6.Text) : 0;
            totalTxtMonth += txtMonthT4_7.Text.Length > 0 ? double.Parse(txtMonthT4_7.Text) : 0;
            totalTxtMonth += txtMonthT4_8.Text.Length > 0 ? double.Parse(txtMonthT4_8.Text) : 0;
            totalTxtMonth += txtMonthT4_9.Text.Length > 0 ? double.Parse(txtMonthT4_9.Text) : 0;
            totalTxtMonth += txtMonthT4_10.Text.Length > 0 ? double.Parse(txtMonthT4_10.Text) : 0;
            totalTxtMonth += txtMonthT4_11.Text.Length > 0 ? double.Parse(txtMonthT4_11.Text) : 0;
            totalTxtMonth += txtMonthT4_12.Text.Length > 0 ? double.Parse(txtMonthT4_12.Text) : 0;

            if (double.Parse(lblTotalAmount.Text) >= totalTxtMonth)
            {
                if (lblSeq_t4.Text.IndexOf("|") != -1)
                {
                    string[] str = lblSeq_t4.Text.Split('|');
                    projectBLL.UpdateProjectExpensePlan(str[0], txtMonthT4_1.Text, txtMonthT4_2.Text, txtMonthT4_3.Text, txtMonthT4_4.Text, txtMonthT4_5.Text, txtMonthT4_6.Text, txtMonthT4_7.Text, txtMonthT4_8.Text, txtMonthT4_9.Text, txtMonthT4_10.Text, txtMonthT4_11.Text, txtMonthT4_12.Text, new SsMng().GetUss(SsMng.SsUserType.EmpSeq));
                }
                else
                {
                    projectBLL.InsertProjectExpensePlan(lblSeq_t4.Text, txtMonthT4_1.Text, txtMonthT4_2.Text, txtMonthT4_3.Text, txtMonthT4_4.Text, txtMonthT4_5.Text, txtMonthT4_6.Text, txtMonthT4_7.Text, txtMonthT4_8.Text, txtMonthT4_9.Text, txtMonthT4_10.Text, txtMonthT4_11.Text, txtMonthT4_12.Text, new SsMng().GetUss(SsMng.SsUserType.EmpSeq));
                }
                RadAjaxManager1.Alert("บันทึกเรียบร้อย");
                LoadData();
                clear_t4();
            }
            else
            {
                RadAjaxManager1.Alert("บันทึกไม่ได้ เกินวงเงินที่ของบฯ");
            }
        }
    }
}
